﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTabControl;
using System.Data.SqlClient;
using System.Data;

public partial class PropertyInfo : System.Web.UI.MasterPage
{
    private int _propertyID;

    protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            _propertyID = int.Parse(Request.QueryString["pid"]);
        }

        catch
        {
            _propertyID = 0;
        }

        foreach (Tab t in propertyDetailTabs.Tabs)
        {
            t.NavigateUrl += "?pid=" + _propertyID.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        SetSidebar();

        Page.Header.Title = "PropertyIQ -- Property Details -- 153 South Monroe Avenue, Columbus, OH 43235";
        imgPropertyHeader.ImageUrl = "~/images/house_pic.jpg";
        ltAddress.Text = "153 South Monroe Avenue<br/>Columbus, OH 43235";

        SqlConnection cn = new SqlConnection(YAF.Classes.Config.ConnectionString);
        cn.Open();

        SqlCommand cmd = new SqlCommand("social_claim_info", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@PropertyID", SqlDbType.Int);
        cmd.Parameters["@PropertyID"].Value = _propertyID;


        SqlDataReader dr = cmd.ExecuteReader();

        bool property_claimed;
        if (!dr.Read())
        {
            property_claimed = false;
        }
        else if ((bool)dr[0] == false)
        {
            property_claimed = false;
        }
        else property_claimed = true;
        claimBtn.Visible = false;
        if (property_claimed)
        {
            string user = dr[2].ToString();
            decimal val = Convert.ToDecimal(dr[3].ToString());
            string path = YAF.Classes.Config.Root;
            path = path.Replace("~", HttpContext.Current.Request.ApplicationPath);
            ltClaim.Text = "Owned by: <a href=" + path + "/default.aspx?g=profile&u=" + dr[1].ToString() + ">";
            ltClaim.Text += user;
            ltClaim.Text += "</a><br>List Price: $";
            ltClaim.Text += String.Format("{0:0,0}", val);
        }
        else if (Request.IsAuthenticated)
        {
            claimBtn.Visible = true;
        }
    }

    private void SetSidebar()
    {
        SqlConnection cn = new SqlConnection(YAF.Classes.Config.ConnectionString);
        cn.Open();

        SqlCommand cmd = new SqlCommand("social_claim_sidebar", cn);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataReader dr = cmd.ExecuteReader();

        ltSidebar.Text = "Listed Properties<br />";
        while (dr.Read())
        {
            string user = dr[2].ToString();
            decimal val = Convert.ToDecimal(dr[3].ToString());
            string path = YAF.Classes.Config.Root;
            DateTime date = Convert.ToDateTime(dr[4]);
            path = path.Replace("~", HttpContext.Current.Request.ApplicationPath);
            ltSidebar.Text += "<a href='http://propertyiq.us/Property_Info/Property_Details.aspx?pid=" + dr[0] + "'>" + dr[5] + "</a><br />";
            ltSidebar.Text += "Owned by: <a href=" + path + "/default.aspx?g=profile&u=" + dr[1].ToString() + ">";
            ltSidebar.Text += user;
            ltSidebar.Text += "</a><br>List Price: $";
            ltSidebar.Text += String.Format("{0:0,0}", val);
            ltSidebar.Text += "<br />Date Claimed: " + date.ToShortDateString() + "<br /><br />";
        }
        
    }

    public int PropertyID
    {
        get
        {
            return _propertyID; 
        }
    }

    public void btnUpdate_Click(object sender, System.EventArgs e)
    {
        decimal listPrice = Convert.ToDecimal(listprice.Text);
        DateTime dateClaimed = DateTime.Now;

        SqlConnection cn = new SqlConnection(YAF.Classes.Config.ConnectionString);
        cn.Open();

        SqlCommand usercmd = new SqlCommand("social_userid_get", cn);
        usercmd.CommandType = CommandType.StoredProcedure;

        usercmd.Parameters.Add("@userName", SqlDbType.NVarChar);
        usercmd.Parameters["@userName"].Value = HttpContext.Current.User.Identity.Name.ToString();

        SqlDataReader userdr = usercmd.ExecuteReader();

        if (userdr.Read())
        {
            int userID = Convert.ToInt32(userdr[0]);
            userdr.Close();

            SqlCommand cmd = new SqlCommand("social_claim_property", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("UserID", userID);
            cmd.Parameters.AddWithValue("PropertyID", _propertyID);
            cmd.Parameters.AddWithValue("ListPrice", listPrice);
            cmd.Parameters.AddWithValue("ForSale", true);
            cmd.Parameters.AddWithValue("DateClaimed", dateClaimed);
            cmd.ExecuteNonQuery();
        }
        Page_Load(sender, e);
    }
}